package com.parsifal.bluetoothdemo.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.parsifal.bluetoothdemo.R;

/**
 * Created by yangming on 17-3-31.
 */
public class ClientDialog extends Dialog {

    public static final int ID = 1;

    private ListView listView;

    private EditText editText;

    private Button button;

    private ClientAdapter adapter;

    private View.OnClickListener onClickListener;//取消按钮被点击了的监听器

    public void setButtonOnclickListener(View.OnClickListener onButton1ClickListener) {
        this.onClickListener = onButton1ClickListener;
    }

    public ClientDialog(@NonNull Context context) {
        super(context, R.style.ChoiceDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_client);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        //初始化界面控件
        initView();
        //初始化界面控件的事件
        initEvent();
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        listView = (ListView) findViewById(R.id.lv_content);
        editText = (EditText) findViewById(R.id.et_send_str);
        button = (Button) findViewById(R.id.btn_send);
        adapter = new ClientAdapter(getContext());
        listView.setAdapter(adapter);
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    onClickListener.onClick(v);
                }
            }
        });
    }

    public String getEditText() {
        return editText.getText().toString();
    }

    public void addClientMessage(String msg) {
        adapter.addClientMessage(msg);
    }

    public void addServerMessage(String msg) {
        adapter.addServerMessage(msg);
        listView.setSelection(adapter.getCount() - 1);
    }
}
